const storeCallbackList = []
const middlewareList = []
export default {
  use(middleware) {
    middlewareList.push(middleware)
    return this
  },
  register(cb) {
    storeCallbackList.push(cb)
  },
  dispatch(action) {
    let index = -1
    const self = this
    function next() {
      const middleware = middlewareList[++index]
      middleware ? middleware(action, next) : self._dispatch(action)
    }
    next()
  },
  _dispatch(action) {
    for (let cb of storeCallbackList) {
      cb(action)
    }
  }
}